<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>智能助手</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
        }

        body {
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }

        .chat-container {
            width: 100%;
            max-width: 900px;
            height: 85vh;
            background: white;
            border-radius: 20px;
            box-shadow: 0 15px 50px rgba(0, 0, 0, 0.1);
            display: flex;
            flex-direction: column;
            overflow: hidden;
        }

        .chat-header {
            background: linear-gradient(90deg, #6366f1, #8b5cf6);
            color: white;
            padding: 20px;
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .chat-header-icon {
            font-size: 24px;
        }

        .chat-header-text h1 {
            font-size: 1.5rem;
            font-weight: 600;
        }

        .chat-header-text p {
            font-size: 0.9rem;
            opacity: 0.9;
            margin-top: 4px;
        }

        .chat-messages {
            flex: 1;
            padding: 25px;
            overflow-y: auto;
            display: flex;
            flex-direction: column;
            gap: 20px;
            background: #f8fafc;
        }

        .message {
            max-width: 80%;
            padding: 18px;
            border-radius: 18px;
            line-height: 1.5;
            position: relative;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
            animation: messageAppear 0.3s cubic-bezier(0.17, 0.67, 0.5, 1.2);
        }

        @keyframes messageAppear {
            from {
                opacity: 0;
                transform: translateY(10px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .user-message {
            background: linear-gradient(90deg, #6366f1, #8b5cf6);
            color: white;
            align-self: flex-end;
            border-bottom-right-radius: 5px;
        }

        .assistant-message {
            background: white;
            color: #334155;
            align-self: flex-start;
            border: 1px solid #e2e8f0;
            border-bottom-left-radius: 5px;
        }

        .message-header {
            font-weight: 600;
            margin-bottom: 8px;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .user-message .message-header {
            color: rgba(255, 255, 255, 0.9);
        }

        .assistant-message .message-header {
            color: #8b5cf6;
        }

        .message-content {
            font-size: 16px;
            line-height: 1.6;
        }

        .chat-input-container {
            padding: 20px;
            background: white;
            border-top: 1px solid #e2e8f0;
        }

        .chat-input {
            display: flex;
            gap: 12px;
        }

        .message-input {
            flex: 1;
            padding: 16px 20px;
            border: 2px solid #e2e8f0;
            border-radius: 20px;
            outline: none;
            font-size: 16px;
            transition: all 0.3s ease;
        }

        .message-input:focus {
            border-color: #8b5cf6;
            box-shadow: 0 0 0 3px rgba(139, 92, 246, 0.1);
        }

        .send-button {
            width: 55px;
            height: 55px;
            background: linear-gradient(90deg, #6366f1, #8b5cf6);
            color: white;
            border: none;
            border-radius: 50%;
            cursor: pointer;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 20px;
            transition: all 0.2s ease;
            box-shadow: 0 4px 10px rgba(99, 102, 241, 0.3);
        }

        .send-button:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 15px rgba(99, 102, 241, 0.4);
        }

        .send-button:active {
            transform: translateY(0);
        }

        .session-info {
            padding: 15px 20px;
            background: #f1f5f9;
            border-bottom: 1px solid #e2e8f0;
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-size: 14px;
        }

        .session-id {
            background: #e0e7ff;
            color: #4f46e5;
            padding: 6px 12px;
            border-radius: 20px;
            font-weight: 500;
        }

        .new-session-btn {
            background: #e0e7ff;
            color: #4f46e5;
            border: none;
            padding: 8px 16px;
            border-radius: 20px;
            cursor: pointer;
            font-weight: 500;
            transition: all 0.2s ease;
        }

        .new-session-btn:hover {
            background: #c7d2fe;
        }

        .typing-indicator {
            display: none;
            align-self: flex-start;
            background: white;
            padding: 18px;
            border-radius: 18px;
            border: 1px solid #e2e8f0;
            border-bottom-left-radius: 5px;
        }

        .typing-indicator-content {
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .typing-indicator-dots span {
            height: 10px;
            width: 10px;
            background: #8b5cf6;
            border-radius: 50%;
            display: inline-block;
            margin: 0 2px;
            animation: typing 1.4s infinite ease-in-out;
        }

        .typing-indicator-dots span:nth-child(1) {
            animation-delay: 0s;
        }

        .typing-indicator-dots span:nth-child(2) {
            animation-delay: 0.2s;
        }

        .typing-indicator-dots span:nth-child(3) {
            animation-delay: 0.4s;
        }

        @keyframes typing {
            0%, 60%, 100% {
                transform: translateY(0);
            }
            30% {
                transform: translateY(-5px);
            }
        }

        .welcome-message {
            text-align: center;
            padding: 40px 20px;
            color: #64748b;
        }

        .welcome-message i {
            font-size: 48px;
            color: #8b5cf6;
            margin-bottom: 20px;
        }

        .welcome-message h2 {
            font-size: 24px;
            margin-bottom: 15px;
            color: #334155;
        }

        .welcome-message p {
            font-size: 16px;
            line-height: 1.6;
            max-width: 500px;
            margin: 0 auto;
        }

        @media (max-width: 768px) {
            .chat-container {
                height: 95vh;
                border-radius: 15px;
            }
            
            .message {
                max-width: 90%;
                padding: 15px;
            }
            
            .chat-header {
                padding: 15px;
            }
            
            .chat-messages {
                padding: 15px;
            }
        }
    </style>
</head>
<body>
    <div class="chat-container">
        <div class="chat-header">
            <div class="chat-header-icon">
                <i class="fas fa-robot"></i>
            </div>
            <div class="chat-header-text">
                <h1>智能助手</h1>
                <p>基于阿里云通义千问的AI对话系统</p>
            </div>
        </div>
        
        <div class="session-info">
            <div class="session-id-container">
                会话ID: <span class="session-id" id="sessionId">未创建</span>
            </div>
            <button class="new-session-btn" id="newSessionBtn">
                <i class="fas fa-plus"></i> 新会话
            </button>
        </div>
        
        <div class="chat-messages" id="chatMessages">
            <div class="welcome-message">
                <i class="fas fa-comments"></i>
                <h2>欢迎使用智能助手</h2>
                <p>点击"新会话"按钮开始一个新的对话，我会记住我们之前的对话内容。</p>
            </div>
        </div>
        
        <div class="typing-indicator" id="typingIndicator">
            <div class="typing-indicator-content">
                <div class="typing-indicator-dots">
                    <span></span>
                    <span></span>
                    <span></span>
                </div>
                <div class="typing-indicator-text">助手正在输入...</div>
            </div>
        </div>
        
        <div class="chat-input-container">
            <div class="chat-input">
                <input type="text" class="message-input" id="messageInput" placeholder="输入消息..." disabled>
                <button class="send-button" id="sendButton" disabled>
                    <i class="fas fa-paper-plane"></i>
                </button>
            </div>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const chatMessages = document.getElementById('chatMessages');
            const messageInput = document.getElementById('messageInput');
            const sendButton = document.getElementById('sendButton');
            const sessionIdSpan = document.getElementById('sessionId');
            const newSessionBtn = document.getElementById('newSessionBtn');
            const typingIndicator = document.getElementById('typingIndicator');
            let sessionId = null;
            let eventSource = null;
            newSessionBtn.addEventListener('click', createNewSession);
            sendButton.addEventListener('click', sendMessage);
            messageInput.addEventListener('keypress', function(e) {
                if (e.key === 'Enter') {
                    sendMessage();
                }
            });

            function createNewSession() {
                if (eventSource) {
                    eventSource.close();
                }
                sessionId = 'session_' + Date.now();
                sessionIdSpan.textContent = sessionId;
                messageInput.disabled = false;
                sendButton.disabled = false;
                messageInput.placeholder = "输入消息...";
                messageInput.focus();
                chatMessages.innerHTML = '';
                addMessage('您好！我是您的智能助手。请问有什么我可以帮助您的吗？', 'assistant');
            }

            function sendMessage() {
                const msg = messageInput.value.trim();
                if (msg === '' || sessionId === null) return;
                addMessage(msg, 'user');
                messageInput.value = '';
                typingIndicator.style.display = 'flex';
                chatMessages.scrollTop = chatMessages.scrollHeight;
                const url = `http://localhost:8080/chatmemory/chat?msg=${encodeURIComponent(msg)}&userId=${encodeURIComponent(sessionId)}`;
                eventSource = new EventSource(url);
                const assistantMessageDiv = document.createElement('div');
                assistantMessageDiv.classList.add('message', 'assistant-message');
                const messageHeader = document.createElement('div');
                messageHeader.classList.add('message-header');
                messageHeader.innerHTML = '<i class="fas fa-robot"></i> 智能助手';
                const messageContent = document.createElement('div');
                messageContent.classList.add('message-content');
                messageContent.textContent = '';
                assistantMessageDiv.appendChild(messageHeader);
                assistantMessageDiv.appendChild(messageContent);
                const welcomeMessage = chatMessages.querySelector('.welcome-message');
                if (welcomeMessage) {
                    welcomeMessage.remove();
                }
                typingIndicator.style.display = 'none';
                chatMessages.appendChild(assistantMessageDiv);
                let accumulatedContent = '';

                eventSource.onmessage = function(event) {
                    typingIndicator.style.display = 'none';
                    accumulatedContent += event.data;
                    messageContent.textContent = accumulatedContent;
                    chatMessages.scrollTop = chatMessages.scrollHeight;
                };

                eventSource.onerror = function(event) {
                    eventSource.close();
                    if (accumulatedContent === '') {
                        messageContent.textContent = '抱歉，我遇到了一些问题，请稍后再试。';
                    }
                };
            }
            
            function addMessage(content, sender) {
                // 移除欢迎消息（如果存在）
                const welcomeMessage = chatMessages.querySelector('.welcome-message');
                if (welcomeMessage) {
                    welcomeMessage.remove();
                }
                
                const messageDiv = document.createElement('div');
                messageDiv.classList.add('message');
                messageDiv.classList.add(sender + '-message');
                
                const messageHeader = document.createElement('div');
                messageHeader.classList.add('message-header');
                
                if (sender === 'user') {
                    messageHeader.innerHTML = '<i class="fas fa-user"></i> 您';
                } else {
                    messageHeader.innerHTML = '<i class="fas fa-robot"></i> 智能助手';
                }
                
                const messageContent = document.createElement('div');
                messageContent.classList.add('message-content');
                messageContent.textContent = content;
                
                messageDiv.appendChild(messageHeader);
                messageDiv.appendChild(messageContent);
                
                chatMessages.appendChild(messageDiv);
                chatMessages.scrollTop = chatMessages.scrollHeight;
            }
        });
    </script>
</body>
</html>